<script setup lang="ts">
import type { LoginState } from '#/auth/Login'
import type { Oauth2ThirdClientListVo } from '#/oauth/thirdClient'
import { getLoginCodeApi } from '@/api/auth'
import { getOauth2ThirdClientListApi } from '@/api/oauth/thirdClient'
import { saveOauthLoginInfo } from '@/utils/oauth'
import {
  Lock,
  SwitchButton,
  User,
} from '@element-plus/icons-vue'

const loginFormRef = $ref<FormInstance>()

const formRules = reactive<FormRules<LoginState>>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
  ],
})

let codeImg = $ref('')

const loginState = reactive<LoginState>({
  grant_type: 'oauth:grant-type:password',
  username: 'admin',
  password: '123456',
  loginType: 'admin',
})

const userStore = useUserStoreHook()
const router = useRouter()
let thirdClientList = $ref<Oauth2ThirdClientListVo[]>([])
async function login() {
  const validate = await loginFormRef?.validate()
  if (!validate) {
    Message({
      message: '请完整填写信息',
      type: 'error',
    })
    return
  }
  userStore.login(loginState).then(() => {
    Message({
      message: '登录成功',
      type: 'success',
    })
    router.replace({ path: `${import.meta.env.VITE_ADMIB_PREFIX}` })
  })
}

// 获取验证码
function getCaptcha() {
  getLoginCodeApi('ARITHMETIC').then((res) => {
    codeImg = res.data.img
    loginState.codeKey = res.data.key
  })
}

// 获取第三方登录列表
function getOauth2ThirdClientList() {
  getOauth2ThirdClientListApi().then((res) => {
    thirdClientList = res.data
  })
}

function handleOatherLogin(client: Oauth2ThirdClientListVo) {
  saveOauthLoginInfo({
    loginType: 'admin',
    type: client.code,
    redirectUrl: `${import.meta.env.VITE_ADMIB_PREFIX}`,
  })
  window.location.href = client._authorizationUri ?? ''
}

onMounted(() => {
  getCaptcha()
  getOauth2ThirdClientList()
})
</script>

<template>
  <div class="login-form-container">
    <div class="login-form">
      <img alt="" class="logo" src="/logo.svg">
      <div class="logo-title">
        StarryAdmin
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginState"
        :rules="formRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginState.username"
            placeholder="用户名"
            class="form-item"
          >
            <template #prefix>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginState.password"
            placeholder="密码"
            class="form-item"
            show-password
            type="password"
          >
            <template #prefix>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <div
            class="code"
            un-flex="~ gap-5 justify-center items-center"
            un-w-full
          >
            <el-input
              v-model="loginState.code"
              un-w-full
              placeholder="验证码"
              class="form-item"
            >
              <template #prefix>
                <el-icon>
                  <SwitchButton />
                </el-icon>
              </template>
            </el-input>
            <img
              class="code-img"
              :src="codeImg"
              alt="code"
              @click="getCaptcha()"
            >
          </div>
        </el-form-item>
        <div class="login-handle">
          <div class="left">
            <el-checkbox>7天内免登录</el-checkbox>
          </div>
          <div class="right">
            <el-link :underline="false">
              忘记密码
            </el-link>
          </div>
        </div>
      </el-form>
      <el-button
        type="primary"
        size="large"
        class="login-submit"
        @click="login"
      >
        登陆
      </el-button>
      <el-divider>
        <span class="login-divider-other">其它登陆方式</span>
      </el-divider>
      <div class="login-other">
        <div v-for="item in thirdClientList" :key="item.code" class="other-item" @click="handleOatherLogin(item)">
          <img :src="item.logo" alt="">
          <span>{{ item.name }}</span>
        </div>
      </div>
    </div>
  </div>
  <div class="login-bg">
    <img alt="" class="login-bg-icon" src="@/assets/images/login_bg.svg">
    <img alt="" class="login-bg-icon-bg" src="@/assets/images/login_icon_bg.png">
  </div>
</template>

<style>
html {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
</style>

<style scoped>
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: -1;
  .login-bg-icon {
    position: absolute;
    top: 50%;
    left: 10%;
    transform: translate(0, -50%);
    width: 350px;
  }
  .login-bg-icon-bg {
    height: 100vh;
  }
}

.login-form {
  width: 420px;
  top: 50%;
  transform: translate(0, -50%);
  right: 20%;
  position: fixed;
  padding: 10px 20px 30px 20px;
  border-radius: 10px;
  animation: login-form-animation 0.5s ease-in-out;
  .login-handle {
    display: flex;
    justify-content: space-between;
  }
  .logo {
    width: 100%;
    height: 80px;
    align-items: center;
    justify-content: center;
  }
  .logo-title {
    font-size: 25px;
    text-align: center;
    margin-top: 10px;
    margin-bottom: 20px;
  }
  .login-submit {
    width: 100%;
    margin-top: 10px;
  }
  .form-item {
    display: flex;
    height: 40px;
    width: 100%;
    text-align: center;
  }
  .code {
    display: flex;
    height: 40px;
    .code-img {
      cursor: pointer;
      width: 100px;
      height: 100%;
      padding: 5px;
      box-sizing: border-box;
      border-radius: 5px;
      border: var(--sy-border);
    }
  }
  .login-other {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    gap: 20px;
    .other-item {
      cursor: pointer;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      gap: 10px;
      img {
        width: 30px;
        height: 30px;
      }
      span {
        font-size: 12px;
        color: var(--sy-text-color-secondary);
      }
    }
  }
}

.login-divider-other {
  color: var(--sy-text-color-secondary);
  font-size: 12px;
}

@keyframes login-form-animation {
  0% {
    opacity: 0;
    right: 0;
  }
  50% {
    opacity: 0.8;
    right: 22%;
  }
  100% {
    opacity: 1;
    right: 20%;
  }
}
@media (max-width: 663px) {
  .login-bg {
    img {
      display: none;
    }
  }
  .login-form-container {
    padding: 30px;
    height: 100vh;
    display: flex;
  }
  .login-form {
    width: 100%;
    top: 48%;
    left: 0;
    height: fit-content;
    position: relative;
    animation: login-form-animation-m 0.5s ease-in-out;
    box-shadow: var(--sy-box-shadow-light);
  }
}
@keyframes login-form-animation-m {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
</style>
